package com.vv.service.Impl;

import com.vv.bean.Order;
import com.vv.bean.Orderinfo;
import com.vv.bean.Product;
import com.vv.bean.Vo.OrderVo;
import com.vv.dao.OrderMapper;
import com.vv.service.OrderInfoService;
import com.vv.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper mapper;

    @Autowired
    OrderInfoService orderInfoService;
    /**
     * 需要做三件事情：
     * 往订单表中添加数据
     * 往订单详情中添加数据
     * 往已售商品中添加数据
     * @param orderVo
     */
    @Override
    public void addOrder(OrderVo orderVo) {
        //获得订单
        Order order = orderVo.getOrder();
        System.out.println(order);
        //获得商品
        List<Product> products = orderVo.getProducts();
        //将order存放到订单中
        mapper.insert(order);
        //将数据存到订单详情（需要封装一个List<orderInfo>）
        List<Orderinfo> orderinfos = new ArrayList<>();
        for (Product product : products) {
            Orderinfo orderinfo = new Orderinfo();
            orderinfo.setOrderId(order.getOrderId());
            orderinfo.setProductId(product.getPid());
            orderinfos.add(orderinfo);
        }
        orderInfoService.addOrderInfo(orderinfos);
    }




}
